<?php
class shoppingcart_model extends CI_Model{
	function shoppingcart_model(){
		parent::__construct();
	}
	
	function getOrderAccessCode($orderid){
		$accessCode = mysql_real_escape_string(base64_encode(sha1($orderid . time())));
        $accessCode = str_replace('=','', $accessCode);
//		$order_code = array('accessCode' => mysql_real_escape_string($accessCode));
//		$this->db->where('id', $orderid);
//		$this->db->update('orders', $order_code);
		return $accessCode;
	}
	
	function getOrders($options){
		$this->db->from('k_orders od');
		if(!empty($options['order_id'])){
			$this->db->where('od.id', $options['order_id']);	
		}
		$this->db->select("od.*");
		
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
			if(empty($options['get_list'])) $rs = $query->first_row();
			else $rs = $query->result();	
		}
		return $rs;
	}
	function getOrderDetails($options){
		$this->db->from('k_order_detail od');
		if(!empty($options['order_id'])){
			$this->db->where('od.order_id', $options['order_id']);	
		}
		$this->db->join('k_deal d', 'd.id = od.deal_id');
		
		$this->db->select("od.*, d.picture2 as picture");
		
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}
		return $rs;
	}
	
	function countSearchOrders($options){
		$this->db->from('orders ord');
		$this->addSearchOrderCondition($options);
		$rs = $this->db->count_all_results();
		return $rs;
	}
	
	function searchOrders($options){
		$this->db->from('orders ord');
		$this->db->join('addresses addr', 'addr.id = ord.shipping_address_id');
		$this->db->select("addr.first_name, addr.last_name, ord.*");
		
		$this->addSearchOrderCondition($options);
		
		if(!empty($options['limit'])){
			if(!empty($options['offset'])){
				$this->db->limit($options['limit'], $options['offset']);
			}else{
				$this->db->limit($options['limit']);
			}
		}
		
		$query = $this->db->get();
		$rs = array();	
		if($query->num_rows() > 0) {
			$rs = $query->result();	
		}		
		return $rs;
	}
	
	function addSearchOrderCondition($options){
		if(!empty($options['frdate'])){
			$this->db->where('CAST(ord.created_at as date)>=', $options['frdate']);	
		}
		if(!empty($options['todate'])){
			$this->db->where('CAST(ord.created_at as date)<=', $options['todate']);	
		}
		
		if(!empty($options['user_id'])){
			$this->db->where('user_id', $options['user_id']);	
		}
		
	}
}
	
?>